import { Page } from '@/base/Page';
import { Family } from '@/base/model/Family';
import { isEmpty } from '@/base/util/Strings';
import {getFamilyList} from '@/base/api/FamilyApi';

class Form {
  familyname = '';
  nickname = '';
  createBegin = '';
  createEnd = '';
  created = [];
  phone = '';
  // companyId = [];
}

export class ListBloc {
  form = new Form();

  paged: Page<Family> = new Page<Family>([], 0, 1, 15, 0, []);
  loading = false;

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.created) && !isEmpty(String(this.form.created[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.created[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.created[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getFamilyList({
      pageIndex,
      pageSize,
      familyname: this.form.familyname,
      nickname : this.form.nickname,
      phone : this.form.phone,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  // removeItem(dateItem: Member) {
  //   this.loading = true;
  //   return deleteMember(dateItem.id)
  //     .finally(() => {
  //       this.loading = false;
  //     });
  // }

  reset() {
    this.form.created = [];
    this.form.createBegin = '';
    this.form.createEnd = '';
    this.form.familyname = '';
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.form.nickname = '';
    this.form.phone = '';
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }

  // getCompanyName(companyId: number) {
  //   const item = this.paged.extras.find(x => x.id === companyId);
  //   return item == null ? '' : item.name;
  // }

}
